package cn.flt.sfisco;

import java.math.BigInteger;

import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.channel.client.P12Manager;
import org.fisco.bcos.helloWorldPrecompiled.contract.HelloWorldPrecompiled;
import org.fisco.bcos.web3j.crypto.ECKeyPair;
import org.fisco.bcos.web3j.crypto.gm.GenCredential;
//import org.fisco.bcos.web3j.precompile.cns.CnsService
//import org.fisco.bcos.asset.contract.Asset;
//import org.fisco.bcos.asset.contract.Asset.RegisterEventEventResponse;
//import org.fisco.bcos.asset.contract.Asset.TransferEventEventResponse;
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.web3j.crypto.Credentials;
import org.fisco.bcos.web3j.precompile.cns.CNS;
import org.fisco.bcos.web3j.precompile.cns.CnsService;
import org.fisco.bcos.web3j.protocol.Web3j;
import org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService;
import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
@Logger
@Slf4j
public class FiscoRunner implements CommandLineRunner {
    //准备部署和调用合约的参数
    private final BigInteger gasPrice = new BigInteger("300000000");
    private final BigInteger gasLimit = new BigInteger("300000000");

    @Override
    public void run(String... args) throws Exception {
        //读取配置文件，SDK与区块链节点建立连接
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //加载账户，获取公钥和私钥
        P12Manager p12 = context.getBean(P12Manager.class);
        //提供密码获取ECKeyPair，密码在生产p12账户文件时指定
        ECKeyPair p12KeyPair = p12.getECKeyPair();
        //以十六进制串输出私钥和公钥
        log.info("p12 privateKey: " + p12KeyPair.getPrivateKey().toString(16));
        log.info("p12 publicKey: " + p12KeyPair.getPublicKey().toString(16));
        //生成Web3SDK使用的Credentials
        Credentials credentials = GenCredential.create(p12KeyPair.getPrivateKey().toString(16));
        log.info("p12 Address: " + credentials.getAddress());
        Service service = context.getBean(Service.class);//获取 service
        service.run(); //创建一个活跃链接
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        //channelEthereumService.setTimeout(10000); 设置交易超时时间为100000毫秒，即100秒
        Web3j web3j = Web3j.build(channelEthereumService, service.getGroupId()); //获取Web3j对象
        //通过Web3j对象调用API接口getBlockNumber
        BigInteger blockNumber = web3j.getBlockNumber().send().getBlockNumber();
        log.info(blockNumber.toString());
        // call HelloWorldPrecompiled  0x5001 get
        System.out.println("------------- WEB3J INIT OK !!! -------------");
        HelloWorldPrecompiled contract = HelloWorldPrecompiled.load(
                "0x0000000000000000000000000000000000005001", web3j, credentials,
                new StaticGasProvider(gasPrice, gasLimit));
        log.info("HelloWorldPrecompiled get() "+contract.get().send());
    }
}
